/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.android.apps.forscience.whistlepunk.audiogen.voices;

/**
 * ************ * WARNING - this code automatically generated by Syntona. * The real source is
 * probably a Syntona patch. * Do NOT edit this file unless you copy it to another directory and
 * change the name. * Otherwise it is likely to get clobbered the next time you * export Java source
 * code from Syntona. * * Syntona is available from: http://www.softsynth.com/syntona/
 */
import com.jsyn.ports.UnitInputPort;
import com.jsyn.ports.UnitOutputPort;
import com.jsyn.unitgen.EnvelopeDAHDSR;
import com.jsyn.unitgen.PassThrough;
import com.jsyn.unitgen.SineOscillator;
import com.softsynth.shared.time.TimeStamp;

public class SineEnvelope extends SimpleJsynUnitVoiceBase {
  // Declare units and ports.
  PassThrough frequencyPassThrough;
  public UnitInputPort frequency;
  PassThrough amplitudePassThrough;
  public UnitInputPort amplitude;
  PassThrough outputPassThrough;
  public UnitOutputPort output;
  SineOscillator sineOsc;
  EnvelopeDAHDSR dAHDSR;

  // Declare inner classes for any child circuits.

  public SineEnvelope() {
    // Create unit generators.
    add(frequencyPassThrough = new PassThrough());
    addPort(frequency = frequencyPassThrough.input, "frequency");
    add(amplitudePassThrough = new PassThrough());
    addPort(amplitude = amplitudePassThrough.input, "amplitude");
    add(outputPassThrough = new PassThrough());
    addPort(output = outputPassThrough.output, "output");
    add(sineOsc = new SineOscillator());
    add(dAHDSR = new EnvelopeDAHDSR());
    // Connect units and ports.
    frequencyPassThrough.output.connect(sineOsc.frequency);
    amplitudePassThrough.output.connect(sineOsc.amplitude);
    sineOsc.output.connect(dAHDSR.amplitude);
    dAHDSR.output.connect(outputPassThrough.input);
    // Setup
    frequency.setup(40.0, 698.4584691287101, 8000.0);
    amplitude.setup(0.0, 0.999969482421875, 1.0);
    dAHDSR.input.set(0.0);
    // Sum of these times should not exceed MIN_TIME_VALUE_CHANGE_MS
    dAHDSR.delay.set(0.01);
    dAHDSR.attack.set(0.01);
    dAHDSR.hold.set(0.04);
    dAHDSR.decay.set(0.01);
    dAHDSR.sustain.set(0.045);
    dAHDSR.release.set(0.01);
  }

  public EnvelopeDAHDSR getDAHDSR() {
    return dAHDSR;
  }

  public void noteOn(double frequency, double amplitude, TimeStamp timeStamp) {
    this.frequency.set(frequency, timeStamp);
    this.amplitude.set(amplitude, timeStamp);
    dAHDSR.input.on(timeStamp);
  }

  public void noteOff(TimeStamp timeStamp) {
    dAHDSR.input.off(timeStamp);
  }

  public UnitOutputPort getOutput() {
    return output;
  }
}
